﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using KeyCore.Domain.Model;
using KeyCore.Framework.Contexts;
//using KeyCore.Framework.Repositories;
using KeyCore.Loans.Client.Web.Models.LoansClient;
using Microsoft.Web.Mvc;

namespace KeyCore.Loans.Client.Web.Controllers
{

    [Authorize]
    public class LoansClientController : Controller
    {

    protected LoansClientRegisterWizardModel  _dataWizard; 

        //
        // GET: /LoansClient/
        public ActionResult Index()
        {
            var wizard  =  new LoansClientRegisterWizardModel();
            wizard.Initialize();
            return View(wizard);

            //using (var crepo = new LoansClientRepository())
            //{
            //    //if (crepo.ClientExist)
            //    //{
            //    //    if (crepo.ClientPersonalDataExist)
            //    //        return RedirectToAction("Index", "Home");
            //    //    return RedirectToAction("CreateLoansClientPersonalData", "LoansClient");
            //    //}
            //    return RedirectToAction("CreateLoansClient", "LoansClient");
            //}
            
        }

        [HttpPost]
        public ActionResult Index([Deserialize] LoansClientRegisterWizardModel wizard, ILoansClientWizardStep step)
        {
            wizard.Steps[wizard.CurrentStepIndex] = step;
            
            // same as [Deserialize] deserializing model to wiz variable
            var serializer = new MvcSerializer();
            var value = Request["wizard"];
            var wiz = serializer.Deserialize(value, SerializationMode.Signed);

            //if(!TryValidateModel(step))
            //{
            //}

            //TryUpdateModel(wizard);

            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(Request["next"]))
                {
                    wizard.CurrentStepIndex++;
                }
                else if (!string.IsNullOrEmpty(Request["prev"]))
                {
                    wizard.CurrentStepIndex--;
                }
                else
                {
                    // TODO: we have finished: all the step partial
                    // view models have passed validation => map them
                    // back to the domain model and do some processing with
                    // the results

                    return Content("thanks for filling this form", "text/plain");
                }
            }
            else if (!string.IsNullOrEmpty(Request["prev"]))
            {
                // Even if validation failed we allow the user to
                // navigate to previous steps
                wizard.CurrentStepIndex--;
            }

            return View(wizard);
        }

 
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //var modelState = filterContext.Controller.ViewData.ModelState;

            //var incomingValues = filterContext.Controller.ValueProvider;

            //var keys = modelState.Keys.Where(x => !incomingValues.ContainsPrefix(x));

            //foreach (var key in keys)
            //{    
            //   // modelState[key].Errors.Clear();
            //}

            base.OnActionExecuting(filterContext);
        }

        protected override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
        }
    }

}
